package cn.taofuxn.sale.service.impl;

import cn.taofuxn.sale.bean.TbConsumer;
import cn.taofuxn.sale.bean.TbGoods;
import cn.taofuxn.sale.bean.TbOrder;
import cn.taofuxn.sale.controller.data.AddOrderRequest;
import cn.taofuxn.sale.controller.data.ModifyOrderRequest;
import cn.taofuxn.sale.dao.ConsumerDao;
import cn.taofuxn.sale.dao.GoodsDao;
import cn.taofuxn.sale.dao.OrderDao;
import cn.taofuxn.sale.service.SaleService;
import cn.taofuxn.sale.service.data.base.BasePageResponse;
import cn.taofuxn.sale.service.data.base.BaseResult;
import cn.taofuxn.sale.service.data.base.ResultCodeType;
import cn.taofuxn.sale.service.data.response.OrderInfo;
import org.assertj.core.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.Date;
import java.util.List;

/**
 * ProjectName : GeekSaleSystem
 * PackageName : cn.taofuxn.sale.service.impl
 * Created by Wong
 * Date: 2018/3/16
 * Time: 10:42
 */
@SuppressWarnings("Duplicates")
@Service
public class SaleServiceImpl implements SaleService {
	@Autowired
	private OrderDao m_OrderDao;
	@Autowired
	private ConsumerDao m_ConsumerDao;
	@Autowired
	private GoodsDao m_GoodsDao;
	
	@Override
	public BaseResult getOrders(final int vPage, final int vPageSize, final Date vStartDate, final Date vEndDate) {
		
		PageRequest tPageRequest = new PageRequest(vPage, vPageSize, new Sort("createTime"));
		Page tOrderPage =  m_OrderDao.findAll((vRoot, vCriteriaQuery, vCb) -> {
			Date tEnd = vEndDate;
			
			Predicate tPredicate = null;
			if(null != vStartDate && null == tEnd){
				tEnd = new Date(System.currentTimeMillis());
			}

			if(null != vStartDate && null !=tEnd)
				tPredicate = vCb.between(vRoot.get("createTime"), vStartDate, tEnd);
			
			return tPredicate;
		}, tPageRequest);
		
		
		BasePageResponse tOrdersResponse = new BasePageResponse();
		tOrdersResponse.setTotalPage(tOrderPage.getTotalPages());
		tOrdersResponse.setTotal((int) tOrderPage.getTotalElements());
		
		List<OrderInfo> tOrderLists = tOrdersResponse.getInfos();
		List<TbOrder> tOrders = tOrderPage.getContent();
		for(TbOrder tOrder : tOrders) {
			OrderInfo tInfo = new OrderInfo();
			tInfo.setOrderId(tOrder.getId());
			tInfo.setSumPrice(tOrder.getSumPrice());
			tInfo.setConsumerAddress(tOrder.getComsumer().getAddress());
			tInfo.setConsumerName(tOrder.getComsumer().getName());
			tInfo.setConsumerId(tOrder.getComsumer().getId());
			tInfo.setConsumerPhone(tOrder.getComsumer().getPhone());
			tInfo.setCreateTime(tOrder.getCreateTime());
			tInfo.setGoodsId(tOrder.getGoods().getId());
			tInfo.setGoodsName(tOrder.getGoods().getName());
			tInfo.setGoodsPrice(tOrder.getGoods().getPrice());
			tInfo.setGoodsProvidePrice(tOrder.getGoods().getProviderPrice());
			tInfo.setPostage(tOrder.getPostage());
			tInfo.setFullCutPrice(tOrder.getFullCutPrice());
			tInfo.setNote(tOrder.getNote());
			
			tOrderLists.add(tInfo);
		}
		
		return BaseResult.success(tOrdersResponse);
	}
	
	@Override
	public BaseResult addOrder(final AddOrderRequest vRequest) {
		TbGoods tTbGoods = m_GoodsDao.findOne(vRequest.getGoodsId());
		if(null == tTbGoods){
			return  BaseResult.error(ResultCodeType.GOODS_NOT_FOUND);
		}
		
		TbConsumer tTbConsumer = null;
		if(!Strings.isNullOrEmpty(vRequest.getConsumerId())){
			tTbConsumer = m_ConsumerDao.findOne(vRequest.getConsumerId());
		}
		
		if(null == tTbConsumer){
			tTbConsumer = new TbConsumer();
			
			tTbConsumer.setAddress(vRequest.getConsumerAddress());
			tTbConsumer.setName(vRequest.getConsumerName());
			tTbConsumer.setPhone(vRequest.getConsumerPhone());
		}
		
		TbOrder tTbOrder = new TbOrder();
		tTbOrder.setGoods(tTbGoods);
		tTbOrder.setComsumer(tTbConsumer);
		tTbOrder.setNote(vRequest.getNote());
		tTbOrder.setSumPrice(vRequest.getSumPrice());
		tTbOrder.setPostage(vRequest.getPostage());
		tTbOrder.setFullCutPrice(vRequest.getFullCutPrice());

		TbOrder tOrder = m_OrderDao.save(tTbOrder);
		
		return null == tOrder ? BaseResult.error() : BaseResult.success(tOrder) ;
	}
	
	@Override
	public BaseResult modifyOrder(final ModifyOrderRequest vRequest) {
		TbOrder tTbOrder = m_OrderDao.findOne(vRequest.getOrderId());
		if(null == tTbOrder) {
			return BaseResult.error(ResultCodeType.ORDER_NOT_FOUND);
		}
		
		TbGoods tTbGoods = m_GoodsDao.findOne(vRequest.getGoodsId());
		if(null == tTbGoods){
			return  BaseResult.error(ResultCodeType.GOODS_NOT_FOUND);
		}
		
		TbConsumer tTbConsumer = null;
		if(!Strings.isNullOrEmpty(vRequest.getConsumerId())){
			tTbConsumer = m_ConsumerDao.findOne(vRequest.getConsumerId());
			if(null == tTbConsumer){
				tTbConsumer = new TbConsumer();
				
				tTbConsumer.setAddress(vRequest.getConsumerAddress());
				tTbConsumer.setName(vRequest.getConsumerName());
				tTbConsumer.setPhone(vRequest.getConsumerPhone());
			}
		}
		
		tTbOrder.setSumPrice(vRequest.getSumPrice());
		tTbOrder.setNote(vRequest.getNote());
		tTbOrder.setComsumer(tTbConsumer);
		tTbOrder.setGoods(tTbGoods);
		tTbOrder.setPostage(vRequest.getPostage());
		tTbOrder.setFullCutPrice(vRequest.getFullCutPrice());
		
		return null == m_OrderDao.saveAndFlush(tTbOrder) ? BaseResult.error(): BaseResult.success();
	}
	
	@Override
	public BaseResult deleteOrder(final String vOrderId) {
		m_OrderDao.delete(vOrderId);
		return BaseResult.success();
	}
	
}
